<template>
  <div class="info-list">
    <div class="head">
      <div
        :class="{
          headItem: true,
          headActive: headInfo.currentIndex === index,
        }"
        @click="changeListContent(index)"
        v-for="(item, index) in headInfo.text"
        :key="index"
      >
        {{ item }}
      </div>
    </div>
    <div class="content-list">
      <div
        class="hotList"
        v-if="headInfo.currentIndex === 0"
        v-loading="headInfo.loading"
      >
        <hot-content :news="gameData.news" />
      </div>
      <div
        class="gameJudge"
        v-if="headInfo.currentIndex === 1"
        v-loading="headInfo.loading"
      >
        <game-judge :judge="gameData.judge" />
      </div>
      <div
        class="gamePre"
        v-if="headInfo.currentIndex === 2"
        v-loading="headInfo.loading"
      >
        <game-pre :pre="gameData.pre" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted } from "vue";
import hotContent from "./hotContent";
import gameJudge from "./gameJudge";
import gamePre from "./gamePre";
import { getGameNews, getGameJudge, getGamePre } from "@/service/youxiaData";
const gameData = reactive({
  news: [],
  judge: [],
  pre: [],
});
const headInfo = reactive({
  text: ["热点资讯", "游戏测评", "游戏前瞻"],
  currentIndex: 0,
  loading: false,
});

onMounted(() => {
  getGameNewsData();
});

// tab转换事件
const changeListContent = (index) => {
  headInfo.currentIndex = index;
  if (index === 0) {
    getGameNewsData();
  }
  if (index === 1) {
    getGameJudgeData();
  }
  if (index === 2) {
    getGamePreData();
  }
};

// 获取数据
const getGameNewsData = async () => {
  headInfo.loading = true;
  const res = await getGameNews();
  gameData.news = res.data;
  headInfo.loading = false;
};

const getGameJudgeData = async () => {
  headInfo.loading = true;
  const res = await getGameJudge();
  gameData.judge = res.data;
  headInfo.loading = false;
};

const getGamePreData = async () => {
  headInfo.loading = true;
  const res = await getGamePre();
  gameData.pre = res.data;
  headInfo.loading = false;
};
</script>

<style scoped lang="less">
.info-list {
  width: 100%;
  margin-left: 10px;
  .head {
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: #f8f8f8;
    box-shadow: 0px 8px 8px 0px rgb(215 215 215 / 40%);
    border-radius: 10px 10px 0px 0px;
    height: 55px;
  }
  .headItem {
    position: relative;
    cursor: pointer;
    transition: all 0.2s;
  }
  .headActive {
    font-size: 18px;
    font-weight: bold;
    &::after {
      content: "";
      width: 50px;
      height: 8px;
      background-color: #f76341;
      border-radius: 4px;
      position: absolute;
      bottom: -20px;
      left: 0;
      right: 0;
      margin: auto;
      display: block;
    }
  }
  .content-list {
    padding: 15px;
    box-shadow: 0px 0px 10px 0px rgb(218 207 204 / 60%);
  }
}
</style>
